package com.hellomsg.controller;

import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

/**
 * Created by mjitech on 2017/6/24.
 */
@Controller
public class FileDownController {

    @RequestMapping(value = "/download", method = RequestMethod.GET)
    public ResponseEntity<byte[]> download() {
        try {
            Thread.sleep(5000);
        } catch (Exception e) {

        }
        File file = new File("/Users/mjitech/Downloads/VMware-Fusion-8.5.7-5528452.dmg");

        try (FileInputStream inputStream = new FileInputStream(file)) {
            byte[] data = new byte[(int)file.length()];
            int length = inputStream.read(data);
            try {
                Thread.sleep(5000);
            } catch (Exception e) {

            }
            HttpHeaders headers = new HttpHeaders();
            headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
            headers.setContentDispositionFormData("attachment", "usb-1_0-fr-spec.pdf");
            return new ResponseEntity<byte[]>(data,
                    headers, HttpStatus.CREATED);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
}
